<template>
	<view class="main-container">
		<view class="content-center" style="margin-top: 20vh;">
			<image src="../../static/logo.jpg" style="width: 250rpx;height: 250rpx;"></image>
		</view>
		<view class="content-center" style="margin-top: 5vh;">
			<view style="font-size: 34rpx;">欢迎使用安排一下</view>
		</view>
		<view class="content-center" style="margin-top: 10vh;">
			<button v-if="canUseGetUserProfile" class="btn_bottom bottom-change-view" @tap="getUserProfile"
				:disabled="submitUserProfileBtn">登录</button>
			<button v-else open-type="getUserInfo" @bindgetuserinfo="handleLogin()"
				class="btn_bottom bottom-change-view" @click="handleLogin()">登录</button>

		</view>
		<view class="content-center" style="margin-top: 5vh;">
			<button class="btn_bottom bottom-change-view" @click="index(8)">取消</button>
		</view>
		<uni-popup ref="getWechatMobilePopup" type="center" animation="false">
			<uni-popup-dialog content="安排一下需要获取您的手机号" type="info">
				<template #buttonContainer>
					<view class="popup-operation">
						<button class="popup-operation-botton" @click="mobilePopupCancel">取消</button>
						<view class="line"></view>
						<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"
							class="popup-operation-botton">确定</button>
					</view>
				</template>
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		loginWithAccount,
		updateCustomerPhone,
		getCustomerWeChatPermission,
		getEnableYuewanSwitch,
		getLogin
	} from '../../constant/login.js';
	import {
		baseImageUrl,
		wechatAppVersion
	} from '../../config/config.js';
	import {
		errorToShow
	} from '@/config/common.js';
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue';
	export default {
		components: {
			uniPopupDialog
		},
		data() {
			return {
				base_image_url: baseImageUrl,
				canUseGetUserProfile: false,
				submitUserProfileBtn: false
			};
		},

		onLoad(e) {
			this.initPage();
			uni.clearStorageSync();
			this.getEnableYuewanSwitch();
		},
		watch: {},
		methods: {
			async index() {
				uni.reLaunch({
					url: '../index/index'
				});
			},
			async initPage() {

				uni.getUserProfile({
					desc: '用户登录', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: infoRes => {
						this.canUseGetUserProfile = true;
					},
					fail: function(loginRes) {
						console.log('获取微信用户信息失败', loginRes);
					}
				});

			},
			async getEnableYuewanSwitch() {
				try {
					let res = await getEnableYuewanSwitch({
						wechatAppVersion: wechatAppVersion
					});
					uni.setStorageSync('wetchatEnableYueWan', res);
				} catch (e) {}
			},
			handleLogin(param) {
				var that = this;
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes)
						if (loginRes.code) {
							let res_code = loginRes.code;
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
									let loginInfo = {
										code: res_code,
										rawData: infoRes.rawData,
										signature: infoRes.signature,
										city: infoRes.userInfo.city,
										province: infoRes.userInfo.province,
										encryptedData: infoRes.encryptedData,
										iv: infoRes.iv,
										nickName: infoRes.userInfo.nickName,
										gender: infoRes.userInfo.gender,
										avatarUrl: infoRes.userInfo.avatarUrl,
										type: 34,
										state: 'default'
									};
									that.wechatLogin(loginInfo);
								}
							});
						}
					},
					fail: function(loginRes) {
						console.log('获取微信用户信息失败', loginRes);
					}
				});
			},
			getUserProfile() {
				var that = this;
				let code = '';
				try {
					this.submitUserProfileBtn = true;
					uni.login({
						success: res => {
							code = res.code;
						}
					});
					uni.getUserProfile({
						desc: '用户登录', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: infoRes => {
							let loginInfo = {
								code: code,
								// rawData: infoRes.rawData,
								// signature: infoRes.signature,
								city: infoRes.userInfo.city,
								province: infoRes.userInfo.province,
								// encryptedData: infoRes.encryptedData,
								// iv: infoRes.iv,
								nickName: infoRes.userInfo.nickName,
								gender: infoRes.userInfo.gender,
								headImg: infoRes.userInfo.avatarUrl,
								type: 34,
								state: 'default'
							};
							that.wechatLogin(loginInfo);
						},
						fail: function(loginRes) {
							console.log('获取微信用户信息失败', loginRes);
						}
					});
				} finally {
					setTimeout(function() {
						that.submitUserProfileBtn = false;
					}, 1000);
				}
			},
			async wechatLogin(obj) {
				let res = await getLogin(obj);
				//	uni.setStorageSync('customerId', res.customer.id);
				uni.setStorageSync('userToken', res.accessToken);
				//uni.setStorageSync('customerInfo', res.customer);
				// await this.getCustomerWeChatPermission();
				// if (!res.Phone) {
				// 	this.openWechatMobilePopup();
				// 	return;
				// }
				uni.reLaunch({
					url: '../mine/index'
				});
			},
			async getCustomerWeChatPermission() {
				try {
					let res = await getCustomerWeChatPermission();
					uni.setStorageSync('userPermission', res);
				} catch (e) {}
			},
			async mobilePopupCancel() {
				this.$refs.getWechatMobilePopup.close();
				let customerId = uni.getStorageSync('customerId');
				let userToken = uni.getStorageSync('userToken');
				if (customerId && userToken) {
					uni.reLaunch({
						url: '../mine/index'
					});
				} else {
					errorToShow('登陆过期，重新登陆');
				}
			},
			getPhoneNumber: function(e) {
				if (e.detail.errMsg != 'getPhoneNumber:fail user deny') {
					let code = '';
					uni.login({
						success: res => {
							code = res.code;
							let postParm = {
								code: code,
								iv: e.detail.iv,
								encryptedData: e.detail.encryptedData
							};
							this.updateCustomerPhone(postParm);
						}
					});
				}
			},
			async updateCustomerPhone(obj) {
				try {
					let res = await updateCustomerPhone(obj);
					let customerInfo = uni.getStorageSync('customerInfo');
					customerInfo.Phone = res;
					uni.setStorageSync('customerInfo', customerInfo);
					this.mobilePopupCancel();
				} catch {
					this.mobilePopupCancel();
				}
			},
			openWechatMobilePopup() {
				this.$refs.getWechatMobilePopup.open();
			}
		}
	};
</script>
<style>
	@import url('../../css/common.css');
</style>

<style scoped>
	button::after {
		content: " ";
		border-radius: unset;
		border: unset;
	}

	button[disabled]:not([type]) {
		color: #FFFFFF;
	}
</style>

<style lang="scss">
	.main-container {
		height: 100vh;
		overflow: auto;
	}

	.content-center {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.popup-operation {
		height: 80rpx;
		display: flex;
		justify-content: space-around;
		width: 100%;
		align-items: center;

		&-botton {
			background: unset;
			color: #007aff;
			border: unset;
			font-size: $main-text-size-24;
		}

		.line {
			width: 2rpx;
			background-color: $main-line-color-grid;
			height: 80%;
		}

		// 边框样式
		button::after {
			border: 0;
		}
	}
</style>